package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.Utils.HeaderMap;
import com.hundsun.yr.universal.library.base.BaseWebActivity;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.library.utils.MathUtils;
import com.hundsun.yr.universal.library.widget.HSButton;
import com.hundsun.yr.universal.library.widget.HSEditText;
import com.hundsun.yr.universal.p2p.bean.MineInvestProtocolBean;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.Extras;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.presenter.base.RequestPresenter;
import com.hundsun.yr.universal.p2p.presenter.impl.InitializePresenterImpl;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.utils.Logs;
import com.hundsun.yr.universal.p2p.view.base.InitializeView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * 还款授权
 */
public class RepaymentAuthorizationActivity extends BaseActivity implements View.OnClickListener, InitializeView {

    /**
     * 授权金额
     */
    private HSEditText hset_authorization_amount = null;

    /**
     * 授权有效期
     */
    private HSEditText hset_authorization_data = null;

    /**
     * 签订授权
     */
    private HSButton btn_empowerment = null;

    /**
     * 网络请求
     */
    private RequestPresenter mPresenter = null;
    private String zwgt_data;

    private int repayment_max_amt;
    private int repayment_min_amt;
    private int repayment_max_deadline;
    private int repayment_min_deadline;

    private TextView tv_tips = null;


    @Override
    protected boolean isApplyKitKatTranslucency() {
        return true;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_pay_authrization;
    }

    @Override
    protected void initViewsAndEvents() {
        findView();
        mPresenter = new InitializePresenterImpl(mContext, this);
        toolbar_title.setText(getString(R.string.repayment_authorization));
        btn_empowerment.setOnClickListener(this);
        tv_tips.setText(R.string.pre_auth_tips);
        hset_authorization_amount.setHint(repayment_min_amt + "~" + repayment_max_amt);
        hset_authorization_data.setHint(repayment_min_deadline + "~" + repayment_max_deadline);
    }

    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.common_toolbar).init();
    }

    public void findView() {
        hset_authorization_amount = findView(R.id.hset_authorization_amount);
        hset_authorization_data = findView(R.id.hset_authorization_data);
        btn_empowerment = findView(R.id.btn_empowerment);
        tv_tips = findView(R.id.tv_tips);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        repayment_max_amt = extras.getInt(Extras.REPAYMENT_MAX_AMT);
        repayment_min_amt = extras.getInt(Extras.REPAYMENT_MIN_AMT);
        repayment_max_deadline = extras.getInt(Extras.REPAYMENT_MAX_DEADLINE);
        repayment_min_deadline = extras.getInt(Extras.REPAYMENT_MIN_DEADLINE);
    }

    @Override
    protected void onEveComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_empowerment:
                if (CommTool.isFastDoubleClick()) {
                    if (checkAmountAndDate()) {
                        zwgt_data = BaseRequestTool2.getFormatedDateString();
                        JSONObject map = new JSONObject();
                        try {
                            map.put("max_amt", hset_authorization_amount.getText().toString());
                            map.put("deadline", hset_authorization_data.getText().toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        Logs.e("xushunzhen: " + hset_authorization_amount.getText().toString() + ", " + hset_authorization_data.getText().toString());

                        JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
                        mPresenter.loadingData(TaskConstants.TASK_REPAYAUTHPAGE, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_user_test)
                                + TaskConstants.TASK_REPAYAUTHPAGE, Constants.EVENT_DATA, jsonParams, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, mContext.getString(R.string.api_user_test)
                                + TaskConstants.TASK_REPAYAUTHPAGE, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), true);
                    }
                }
                break;
        }
    }

    @Override
    public <T> void initializeViews(String task_name, T data) {
        switch (task_name) {
            case TaskConstants.TASK_REPAYAUTHPAGE:
                MineInvestProtocolBean rechargeBean = (MineInvestProtocolBean) data;

                zwgt_data = BaseRequestTool2.getFormatedDateString();
                HashMap<String, String> hearder = BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, mContext.getString(R.string.h5_user) +
                        TaskConstants.TASK_REPAYAUTHPAGERST, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data);
                HeaderMap headerMap = new HeaderMap();
                headerMap.setMap(hearder);
                Bundle bundle = new Bundle();
                bundle.putString(BaseWebActivity.BUNDLE_KEY_URL, rechargeBean.html);
                bundle.putSerializable(BaseWebActivity.BUNDLE_KEY_HEADER, headerMap);
                bundle.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, BaseWebActivity.LoadType.DATA.getIntValue());
                readyGoThenKill(WebOpenAccountActivity.class, bundle);
                break;
        }
    }

    @Override
    public <T> void initializeError(String task_name, T data) {

    }

    /**
     * 检查输入格式
     */
    private boolean checkAmountAndDate() {
        if (hset_authorization_amount.getText().toString().isEmpty()) {
            showToast(getString(R.string.please_enter_authorization_amount));
            return false;
        }

        if (hset_authorization_data.getText().toString().isEmpty()) {
            showToast(getString(R.string.please_enter_effective_date));
            return false;
        }

        if (MathUtils.compare(hset_authorization_amount.getText().toString(), repayment_max_amt + "") > 0) {
            showToast("输入授权金额过大");
            return false;
        }

        if (MathUtils.compare(hset_authorization_amount.getText().toString(), repayment_min_amt + "") < 0) {
            showToast("输入授权金额过小");
            return false;
        }

        if (MathUtils.compare(hset_authorization_data.getText().toString(), repayment_max_deadline + "") > 0) {
            showToast("输入生产日期过大");
            return false;
        }

        if (MathUtils.compare(hset_authorization_data.getText().toString(), repayment_min_deadline + "") < 0) {
            showToast("输入生产日期过小");
            return false;
        }

        if (!(sharedPreferences.getShareProf(UserInfo.IF_ACCOUNT) != null && TextUtils.equals(sharedPreferences.getShareProf(UserInfo.IF_ACCOUNT), "YES"))) {
            showToast(getString(R.string.open_account));
            return false;
        }

        if (!sharedPreferences.getShareProf(UserInfo.IF_PAYPWD).equals("YES")) {
            showToast(R.string.set_paypwd_yet);
            return false;
        }

        return true;
    }


}
